namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class votes
    {
        // Methods
        public static int Add(vote model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into votes(");
            builder.Append("categoryid,parentid,username,[name],[time],[count],[visible]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.categoryid + ",");
            builder.Append("" + model.parentid + ",");
            builder.Append("'" + model.username + "',");
            builder.Append("'" + model.name + "',");
            builder.Append("'" + model.time + "',");
            builder.Append("" + model.count + ",");
            builder.Append("" + (model.visible ? 1 : 0) + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            string sql = "SELECT @@IDENTITY AS Expr";
            object scalar = connection.GetScalar(sql);
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return int.Parse(scalar.ToString());
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete votes ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            int num;
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from votes where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                num = 0;
            }
            else
            {
                num = int.Parse(scalar.ToString());
            }
            if (num == 0)
            {
                return false;
            }
            return true;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from votes ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static vote GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from votes ");
            builder.Append(" where id=" + id);
            vote vote = new vote();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            vote.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            if (dataTable.Rows[0]["categoryid"].ToString() != "")
            {
                vote.categoryid = int.Parse(dataTable.Rows[0]["categoryid"].ToString());
            }
            if (dataTable.Rows[0]["parentid"].ToString() != "")
            {
                vote.parentid = int.Parse(dataTable.Rows[0]["parentid"].ToString());
            }
            vote.name = dataTable.Rows[0]["name"].ToString();
            if (dataTable.Rows[0]["time"].ToString() != "")
            {
                vote.time = DateTime.Parse(dataTable.Rows[0]["time"].ToString());
            }
            if (dataTable.Rows[0]["count"].ToString() != "")
            {
                vote.count = int.Parse(dataTable.Rows[0]["count"].ToString());
            }
            if (dataTable.Rows[0]["visible"].ToString() != "")
            {
                if ((dataTable.Rows[0]["visible"].ToString() == "1") || (dataTable.Rows[0]["visible"].ToString().ToLower() == "true"))
                {
                    vote.visible = true;
                    return vote;
                }
                vote.visible = false;
            }
            return vote;
        }

        public static void Update(vote model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update votes set ");
            builder.Append("categoryid=" + model.categoryid + ",");
            builder.Append("parentid=" + model.parentid + ",");
            builder.Append("[username]='" + model.username + "',");
            builder.Append("[name]='" + model.name + "',");
            builder.Append("[time]='" + model.time + "',");
            builder.Append("[count]=" + model.count + ",");
            builder.Append("[visible]=" + (model.visible ? 1 : 0) + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }


}

